import { observable, action } from 'mobx';
import { WeaTableNew, WeaForm } from 'comsMobx';

const { TableStore } = WeaTableNew;
import { message, Modal } from 'antd';
import * as Apis from '../apis/customercard';
import { crmAddFormRules } from '../util/index';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;
export class AddressManageStore {
    @observable rightMenu = [];
    @observable loading = false;
    @observable verified = false;
    @observable hasRight = false;
    @observable tableStore = new TableStore();
    @observable customerId = '';
    @observable form = new WeaForm();
    @observable formCondition = [];
    @observable operate = '';
    @observable showModal = false;
    @observable addressId = '';

    @action
    getViewAddressList = (params = {}) => {
      Apis.getViewAddressList(params).then((data) => {
        if (data.status == 'success') {
          if (!data.hasright && typeof (data.hasright) !== 'undefined') {
            this.hasRight = false;
            this.verified = true;
          } else {
            this.hasRight = true;
            this.verified = true;
            this.tableStore.getDatas(data.sessionkey, params.current || 1);
            this.dataKey = data.sessionkey;
            this.customerId = params.customerId;
          }
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }

    getViewAddressRightMenu=(params) => {
      Apis.getRightMenuList(params).then((data) => {
        if (data.status == 'success') {
          this.rightMenu = data.datas;
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }

    // 删除
    deleteAddress=(params = {}) => {
      const that = this;
      Modal.confirm({
        title: getLabel(15172, '系统提示'),
        content: getLabel(83600, '您确认要删除吗？'),
        okText: getLabel(33703, '确定'),
        cancelText: getLabel(201, '取消'),
        onOk() {
          Apis.deleteAddress(params).then((data) => {
            if (data.status == 'success') {
              that.getViewAddressList({ customerId: that.customerId });
            } else {
              message.error(getLabel(83437, '出现错误！') + data.msgcode);
            }
          });
        },
        onCancel() {},
      });
    }
    // 新建,编辑
    operateAddressForm =(bool, params = {}, type) => {
      this.showModal = bool;
      if (bool) {
        this.form = new WeaForm();
        if (type == 'add') {
          Apis.viewAddressAddForm(params).then((data) => {
            if (data.status == 'success') {
              this.operate = 'add';
              this.formCondition = data.datas;
              this.form && !this.form.isFormInit && this.form.initFormFields(crmAddFormRules(data.datas));
            } else {
              message.error(getLabel(83437, '出现错误！') + data.msgcode);
            }
          });
        } else {
          Apis.addressEditForm(params).then((data) => {
            if (data.status == 'success') {
              this.operate = 'edit';
              this.addressId = params.addressId;
              this.formCondition = data.datas;
              this.form && !this.form.isFormInit && this.form.initFormFields(crmAddFormRules(data.datas));
            } else {
              message.error(getLabel(83437, '出现错误！') + data.msgcode);
            }
          });
        }
      }
    }
    // 保存
    saveAddress = (params = {}) => {
      const formParams = this.form.getFormParams();
      this.form.validateForm().then((f) => {
        if (f.isValid) {
          if (this.operate == 'add') {
            const newParams = { ...formParams, customerId: this.customerId };
            this.loading = true;
            Apis.saveAddressAdd(newParams).then((data) => {
              this.loading = false;
              if (data.status == 'success') {
                this.operateAddressForm(false);
                this.getViewAddressList({ customerId: this.customerId });
              } else {
                message.error(getLabel(83437, '出现错误！') + data.msgcode);
              }
            });
          }
        } else {
          f.showErrors();
        }
      });
    }
    // 编辑 实时保存
    saveEditAddress=(params = {}) => {
      const that = this;
      Apis.saveAddressEditForm(params).then((data) => {
        if (data.status == 'success') {
          message.success(getLabel(83551, '保存成功！'));
          this.getViewAddressList({ customerId: this.customerId });
          that.setFieldValueOld(params.fieldname, params.newvalue);
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }

    setFieldValueOld(fieldName, fieldValue) {
      const conditioninfo = this.formCondition;
      for (let i = 0; i < conditioninfo.length; i++) {
        const items = conditioninfo[i].items;
        for (let j = 0; j < items.length; j++) {
          const item = items[j];
          const name = item.domkey[0];
          if (fieldName == name) {
            item.value = fieldValue;
          }
        }
      }
    }
}
